package tech.wangpai.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.wangpai.domain.User;

import java.util.UUID;

@RestController
public class RedisTemplateController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * http://localhost:8080/set?name=zhangsan&age=12
     * @param name
     * @param age
     * @return
     */
    @GetMapping(value = "/set")
    public String set(String name, Integer age) {
        User user = new User();
        String id = UUID.randomUUID().toString();
        user.setId(id);
        user.setName(name);
        user.setAge(age);
        redisTemplate.opsForValue().set(id, user);
        return id;
    }

    /**
     * http://localhost:8080/get?id=
     * @param id
     * @return
     */
    @GetMapping("/get")
    public User get(String id) {
        return (User) redisTemplate.opsForValue().get(id);
    }
}
